var configXML = "/var/www/includes/config/ProjectStatus.xml";
var TimeLineApp = null;
$(document).ready(function()
{
	TimeLineApp = new App();

	$("a#debugtoggle").click(function()
	{
		$('#debug').toggle();
	});
	$("a#initTimeline").click(function()
	{
		TimeLineApp.initTimeline($("select#scale").val());
		TimeLineApp.renderColumns();
	});
	$("a#rendercolumns").click(function()
	{
		TimeLineApp.renderColumns();
	});
	$("a#addrow").click(function()
	{
		row = TimeLineApp.timeline.addRow({
			id: $("input#rowid").val(),
			caption: $("input#rowcaption").val(),
			height: '50px'
		});
		$("select#itemRowId").append('<option value="'+ $("input#rowid").val() +'">'+ $("input#rowcaption").val()+'</option>');
	});
	$("a#additem").click(function()
	{
		var aMin = $("input#itemStartDate").val().split('-');
		var aMax = $("input#itemEndDate").val().split('-');
		newitem = TimeLineApp.timeline.addItem(
		{
			id: $("input#itemid").val(),
			row: $("select#itemRowId").val(),
			startDate: new Date(aMin[0], parseInt(aMin[1]) - 1, parseInt(aMin[2])),
			endDate: new Date(aMax[0], parseInt(aMax[1]) - 1, parseInt(aMax[2])),
		});
		newitem.html($("textarea#itemhtml").val());
	});
	// This gives us the cool upper right red google like thing
	$("#notice")
	.ajaxStart(function()
	{
		$(this).show();
	})
	.ajaxStop(function()
	{
		$(this).hide();
	});
	
	$("#debug").error( function(e) { 
		alert(e.message); 
	}).ajaxError(function(a,b,c,d)
	{
		alert('message: ' + d.message + "\n************************\nfileName: " + d.fileName + "\n************************\nlineNumber: " + d.lineNumber + "\n************************\nstack: " + d.stack);
	});
	
});

function App()
{
	this.data = null;
	this.timeline= null;
	return this;
}
App.prototype = {
	data: null,
	timeline: null,
	initTimeline: function(dateUnit)
	{
		var time1 = new Date();
		var aMin = $("input#inStartDate").val().split('-');
		var aMax = $("input#inEndDate").val().split('-');
				
		var timelinesettings =
		{
			startDate: new Date(aMin[0], parseInt(aMin[1]) - 1, parseInt(aMin[2])),
			endDate: new Date(aMax[0], parseInt(aMax[1]) - 1, parseInt(aMax[2])),
			dateUnit: dateUnit
		};
		// set to 100 bce: timelinesettings.startDate.setYear(-100); 
		// set to 20 bce: timelinesettings.endDate.setYear(-20);
		$("#debug").append('timelinesettings.startDate: ' + timelinesettings.startDate + "<br />");
		$("#debug").append('timelinesettings.endDate: ' + timelinesettings.endDate + "<br />");
		
		// Now we have what we need to build the timeline, so create it while emptying the parent div
		this.timeline = new Timeline($("#timeline").empty(), timelinesettings);
		var time2 = new Date();
		$("#debug").append('initTimeline time: ' + Math.floor(time2.getTime() -  time1.getTime()) + "ms<br />");
		//this.renderColumns();
		return this;
	},
	renderColumns: function()
	{
		var time1 = new Date();
		// render the columns
		this.timeline.renderColumns();
		var time2 = new Date();
		$("#debug").append('renderColumns time: ' + Math.floor(time2.getTime() -  time1.getTime()) + "ms<br />");

		var time1 = new Date();
		return this;
	},
	renderTopics: function()
	{
		var me = this;
		var time1 = new Date();
		with(this.data)
		{
			// process the topics into rows
			$(topics).each(function(i)
			{
				me.timeline.addRow({
					id: topics[i].id,
					caption: topics[i].topic,
					height: '50px'
				});
			});
		}
		var time2 = new Date();
		$("#debug").append('renderTopics time: ' + Math.floor(time2.getTime() -  time1.getTime()) + "ms<br />");
		return this;
	},
	addItem: function()
	{
		var me = this;
		var time1 = new Date();
		var time2 = new Date();
		$("#debug").append('renderItems time: ' + Math.floor(time2.getTime() -  time1.getTime()) + "ms<br />");
		$("#debug").append('$("div#rowsBox div.item").length: ' + $("div#rowsBox div.item").length + "<br />");
		$('div#rowsBox a').ToolTip(
		{
			className: 'linksTooltip',
			position: 'mouse',
			delay: 200
		});
		return this;
	}
};